<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/layouts/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>编辑类别</title>
    <%@ include file="/WEB-INF/layouts/edit-header.jsp" %>
    <%--select--%>
    <link rel="stylesheet" type="text/css" href="${staticPath}/select/css/bootstrap-drilldown-select.css">
    <%--select--%>

    <script type="text/javascript">
        var _ctx = '${ctx}';
        var _staticPath = '${staticPath}';
    </script>

    <%--select--%>
    <script src="${staticPath}/select/js/bootstrap-drilldown-select.js"></script>
    <script src="${staticPath}/admin/tcsf/wg_listToTree1.1.js"></script>

<%--select--%>
</head>
<body>

<div class="create-page tpanel">
    <div class="panel-content">
        <div class="container-fluid">

            <form id="createForm" class="form-horizontal">
                <input type="hidden" name="id" value="${entity.id}"/>

                <div class="form-group form-group-first">
                    <label for="name" class="col-sm-2 control-label">名称</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control"
                               id="name" name="name"
                               value="${entity.name}"
                               placeholder="请输入名称(必填)" required
                               minlength="0" maxlength="100"
                        />
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">父节点</label>
                    <div class="col-sm-8">
                        <input id="drilldown1" class="form-control" placeholder="请选择分类"
                               data-toggle="dropdown" readonly="readonly">
                        <input id="parentId" class="hidden" name="parentId">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-12" style="text-align: center;">
                        <a class="btn btn-default" href="javascript:window.history.back();">返回</a>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <input type="submit" class="btn btn-primary" value="保存"/>
                    </div>
                </div>

            </form>

        </div>
    </div>
</div>
<script type="text/javascript">
    window.parent.changeTitle('编辑类别');
    $(function () {
        $.getJSON('${ctx}/admin/helparticlecatalog/listData', function (result) {
            if (result.code == 'OK') {
                fillData(result.body);
            }
        });


        $('#createForm').validate({
            focusCleanup: true,
            focusInvalid: false,
            errorClass: "unchecked",
            validClass: "checked",
            errorElement: "span",
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    type: "post",
                    url: "${ctx}/admin/helparticlecatalog/update",
                    beforeSubmit: function () {
                        window.parent.layer.load();
                    },
                    success: function (r) {
                        var resp = $.parseJSON(r);
                        window.parent.layer.closeAll('loading');
                        if (resp.code == 'OK') {
                            window.location.href = '${ctx}/admin/helparticlecatalog';
                            window.parent.layer.msg('保存成功', {icon: 1});
                        } else {
                            window.parent.layer.msg(resp.message, {icon: 2});
                        }
                    }
                });
                return false;
            },
            errorPlacement: function (error, element) {
                var s = element.parent().find("span[htmlFor='" + element.attr("id") + "']");
                if (s != null) {
                    s.remove();
                }
                error.appendTo(element.parent());
            },
            success: function (label) {
                label.removeClass("unchecked").addClass("checked");
            }
        });

        $('.datetimepicker').datetimepicker({format: 'yyyy-mm-dd hh:ii:ss'});
    });

    function fillData(data) {
        var param = {
            list: data,
            inParams: {
                pid: "parentId",
                rootId: null,
                id: 'id'
            },
            outParams: {
                children: "list",
                pid: "pid",
                id: 'id'
            },
            sort: {
                orderBy: ["sort"],
                sort: "asc"
            }
        };
        var tree = wg_listToTree(param);
        $('#drilldown1').drilldownSelect({
            appendValue: false,
            data: tree,
            onSelected: function (event) {
                $('#parentId').val($(event.target).data('id'));
                $('#drilldown1').val(event.target.innerHTML);
            }
        });
    }

</script>
</body>
</html>
